package com.snake.springboot.custom.service.impl.base;

import com.github.pagehelper.Page;
import com.snake.springboot.custom.api.model.base.CustSubAccountModel;
import com.snake.springboot.custom.api.request.base.CustSubAccQuery;
import com.snake.springboot.custom.api.request.base.SubAccPwdReq;
import com.snake.springboot.custom.api.request.page.PageQuery;
import com.snake.springboot.custom.api.service.base.CustSubAccountService;
import com.snake.springboot.custom.dao.mybatis.entity.base.CustSubAccountDO;
import com.snake.springboot.custom.dao.mybatis.entity.base.CustomDO;
import com.snake.springboot.custom.dao.mybatis.mappers.base.CustSubAccountMapper;
import com.snake.springboot.custom.dao.mybatis.mappers.base.CustomMapper;
import com.snake.springboot.custom.service.assemble.base.CustSubAccAssembler;
import com.snake.springboot.custom.service.utils.PageUtils;
import org.snake.common.exception.OperationException;
import org.snake.common.result.ReturnList;
import org.snake.common.security.MD5Util;
import org.snake.common.utils.JsonUtils;
import org.snake.common.utils.ListUtils;
import org.snake.common.utils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商户子账户实现类Service
 * 类{@code CustSubAccountServiceImpl} </br>
 * 创建时间 2025/02/08 09:36
 * @author yangxt
 * @see
 */
@Service
public class CustSubAccountServiceImpl implements CustSubAccountService{

    private CustSubAccountMapper custSubAccountMapper;
    private CustomMapper customMapper;
    @Autowired
    public CustSubAccountServiceImpl(CustSubAccountMapper custSubAccountMapper, CustomMapper customMapper) {
        this.custSubAccountMapper = custSubAccountMapper;
        this.customMapper = customMapper;
    }

    @Override
    public CustSubAccountModel addCustSubAccount(CustSubAccountModel model) {
        CustSubAccountDO accountDO = CustSubAccAssembler.subAccDOAss(model,true);
        CustomDO customDO = customMapper.queryByCustomId(accountDO.getCustomId());
        int i =custSubAccountMapper.querySubCount(accountDO.getCustomId());
        OperationException.checkIsTrue(customDO.getSubAllCount() > i,"子账户数量已达到上限");
        CustSubAccountDO subAccountDO = custSubAccountMapper.queryAcc(accountDO.getAccount());
        OperationException.checkIsTrue(subAccountDO==null,"当前账号已被注册");
        custSubAccountMapper.insertSelective(accountDO);
        return model;
    }

    @Override
    public void modifyCustSubAccount(CustSubAccountModel model) {
        CustSubAccountDO accountDO = CustSubAccAssembler.subAccDOAss(model,false);
        custSubAccountMapper.update(accountDO);

    }

    @Override
    public void updateState(CustSubAccountModel updateState) {
        CustSubAccountDO accountDO = CustSubAccAssembler.subAccDOAss(updateState,false);
        custSubAccountMapper.update(accountDO);
    }

    @Override
    public void removeCustSubAccount(String id) {
        custSubAccountMapper.delete(id);
    }

    @Override
    public ReturnList<CustSubAccountModel> queryList(PageQuery<CustSubAccQuery> params) {
        Page<Object> objects = PageUtils.forcePage(params.getPage());
        CustSubAccountDO queryDO=CustSubAccAssembler.subAccQueryAss(params.getParams());
        List<CustSubAccountDO> custSubAccountDOList = custSubAccountMapper.queryCustSubAccountList(queryDO);
        List<CustSubAccountModel> accountModels = ListUtils.copy(custSubAccountDOList, CustSubAccountModel.class);
        return new ReturnList<>(accountModels,objects.getTotal());
    }

    @Override
    public List<CustSubAccountModel> queryList(CustSubAccQuery query) {
        CustSubAccountDO queryDO=CustSubAccAssembler.subAccQueryAss(query);
        List<CustSubAccountDO> custSubAccountDOList = custSubAccountMapper.queryCustSubAccountList(queryDO);
        List<CustSubAccountModel> accountModels = ListUtils.copy(custSubAccountDOList, CustSubAccountModel.class);
        return accountModels;
    }

    @Override
    public void updatePwdByMe(SubAccPwdReq pwdReq) {
        CustSubAccountDO accountDO = new CustSubAccountDO();
        accountDO.setPassword(MD5Util.encrypt(pwdReq.getPassword()));
        accountDO.setSubAccountId(pwdReq.getSubAccountId());
        custSubAccountMapper.updatePwd(accountDO);

    }

    @Override
    public void updatePwdByAdmin(SubAccPwdReq pwdReq) {
        CustSubAccountDO accountDO = new CustSubAccountDO();
        accountDO.setPassword(MD5Util.encrypt(pwdReq.getPassword()));
        accountDO.setSubAccountId(pwdReq.getSubAccountId());
        custSubAccountMapper.updatePwd(accountDO);
    }

    @Override
    public CustSubAccountModel queryDetail(String id) {
        CustSubAccountDO accountDO = custSubAccountMapper.queryById(id);
        CustSubAccountModel model = new CustSubAccountModel();
        PropertyUtils.copyPropertiesIfNull(model,accountDO);
        return model;
    }
}
